package com.lovo.sheyingback.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.sheyingback.bean.Content;
import com.lovo.sheyingback.bean.Image;
import com.lovo.sheyingback.bean.Type;
import com.lovo.sheyingback.dao.ContentMapper;
import com.lovo.sheyingback.dao.ImageMapper;
import com.lovo.sheyingback.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private ContentMapper contentMapper;
    @Autowired
    private ImageMapper imageMapper;
    @Override
    public PageInfo<Content> listContent(Integer current, Integer pageSize, Content content) {
        PageHelper.startPage(current,pageSize);
        List<Content> contents = contentMapper.listContent(content);
        return new PageInfo<>(contents);
    }
    @Override
    public PageInfo<Content> listContentByType(Integer current, Integer pageSize, Integer typeId) {
        PageHelper.startPage(current,pageSize);
        List<Content> contents = contentMapper.listContentByType(typeId);
        return new PageInfo<>(contents);
    }

    @Override
    public Content getById(Integer id) {
        return contentMapper.getById(id);
    }

    @Override
    @Transactional
    public int insert(Content content) {
        content.setCreateDate(LocalDateTime.now());
        content.setUpdateDate(LocalDateTime.now());
        content.setIndex(contentMapper.getMaxIndex(content.getType().getId()));
        contentMapper.insert(content);
        if(content.getImages() != null){
            for(int i = 0;i < content.getImages().size();i++){
                Image image = content.getImages().get(i);
                image.setContent(content);
                image.setIndex(i+1);
                image.setCreateDate(LocalDateTime.now());
                imageMapper.insert(image);
            }
        }

        return 1;
    }

    @Override
    @Transactional
    public int update(Content content) {
        contentMapper.update(content);
        if(content.getImages() != null){
            imageMapper.deleteByContentId(content.getId());
            for(int i = 0;i < content.getImages().size();i++){
                Image image = content.getImages().get(i);
                image.setContent(content);
                image.setIndex(i+1);
                image.setCreateDate(LocalDateTime.now());
                imageMapper.insert(image);
            }
        }
        return 1;
    }

    @Override
    @Transactional
    public int updateIndex(Integer id, Integer val) {
        Content content = contentMapper.getById(id);
        // 修改图文序号时不用修改图片
        content.setImages(null);
        content.setIndex(content.getIndex() +val);
        contentMapper.updateIndex(content.getType().getId(),content.getIndex(),-val);
        contentMapper.update(content);
        return 1;
    }

}
